
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Demo," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>从服务器获取数据，引入组件 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        从服务器获取数据，引入组件
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:36.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Demo/" rel="tag">Ember-Demo</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 8 分钟</span>
    </header>

    <div class="post-content">
      <p>来源：<a target="_blank" rel="noopener" href="http://yoember.com/">yoember.com</a><br>作者：<a target="_blank" rel="noopener" href="http://zoltan.nz/">Zoltan</a></p>
<p><strong>声明</strong>：<em>本文的转载与翻译是经过作者认可的，再次感谢原作，如有侵权请给我留言，我会删除博文！！</em> 希望本系列教程能帮助更多学习Ember.js的初学者。</p>
<p>接着前面四篇：</p>
<ol>
<li><a href="http://xcoding.tech/2016/03/30/Ember-Demo/%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E4%BB%A5%E5%8F%8A%E4%BD%BF%E7%94%A8Ember.js%E5%88%9B%E5%BB%BA%E7%AC%AC%E4%B8%80%E4%B8%AA%E9%9D%99%E6%80%81%E9%A1%B5%E9%9D%A2/">环境搭建以及使用Ember.js创建第一个静态页面</a></li>
<li><a href="http://xcoding.tech/2016/03/31/Ember-Demo/%E5%BC%95%E5%85%A5%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E3%80%81action%E3%80%81%E5%8A%A8%E6%80%81%E5%86%85%E5%AE%B9/">引入计算属性、action、动态内容</a></li>
<li><a href="http://xcoding.tech/2016/03/31/Ember-Demo/%E6%A8%A1%E5%9E%8B%EF%BC%8C%E4%BF%9D%E5%AD%98%E6%95%B0%E6%8D%AE%E5%88%B0%E6%95%B0%E6%8D%AE%E5%BA%93/">模型，保存数据到数据库</a></li>
<li><a href="http://xcoding.tech/2016/03/31/Ember-Demo/%E5%8F%91%E5%B8%83%E9%A1%B9%E7%9B%AE%EF%BC%8C%E5%8A%A0%E5%85%A5CRUD%E5%8A%9F%E8%83%BD/">发布项目，加入CRUD功能</a></li>
</ol>
<h2 id="清理模板，使用组件重构"><a href="#清理模板，使用组件重构" class="headerlink" title="清理模板，使用组件重构"></a>清理模板，使用组件重构</h2><p>2.0版本之后组件会越来越重要。有关组件的介绍请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/ember-js-ru-men-zhi-nan-zhi-er-shi-ba-zu-jian-ding-yi/">Ember.js 入门指南之二十八组件定义</a>。组件的创建同样可以使用<a target="_blank" rel="noopener" href="http://ember-cli.com/user/guide">Ember CLI</a>命令创建。如下命令创建了2个组件，创建的同时会自动创建2个文件；一个是组件类（<code>app/components/xxx.js</code>）。一个是组件对应的模板（<code>app/templates/components/xxx.hbs</code>）。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ember g component library-item</span><br><span class="line">ember g component library-item-form</span><br></pre></td></tr></table></figure>

<p><strong>修改模板library-item</strong></p>
<p>下面在组件模板<code>library-item.hbs</code>中增加如下代码：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- app/templates/components/library-item.hbs --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;panel panel-default library-item&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;panel-heading&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h3</span> <span class="attr">class</span>=<span class="string">&quot;panel-title&quot;</span>&gt;</span>&#123;&#123;item.name&#125;&#125;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;panel-body&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>Address: &#123;&#123;item.address&#125;&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>Phone: &#123;&#123;item.phone&#125;&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;panel-footer text-right&quot;</span>&gt;</span></span><br><span class="line">      &#123;&#123;yield&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>如果注意看可以发现上述代码与<code>app/templates/libraries/index.hbs</code>文件的代码非常相似。这是<code>item</code>替代了<code>model</code>。至于<code>item</code>是怎么来的请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/ember-js-ru-men-zhi-nan-zhi-er-shi-jiu-shu-xing-chuan-di/">Ember.js 入门指南之二十九属性传递</a>，这篇博文介绍了组件的属性传递，<code>item</code>是从调用组件的模板传递过来的。上述代码中还有一个重要的东西是<code>{{yield}}</code>，这个表达式与<code>{{outlet}}</code>类似。同样也是一个占位符。组件渲染之后会被传进来的html代码替换。比如下面的调用代码：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;#library-item item=model&#125;&#125;</span><br><span class="line">  Closed</span><br><span class="line">&#123;&#123;/library-item&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>组件渲染之后，上述的<code>Closed</code>会替换到<code>{{yield}}</code>这里，最终得到的html代码如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;panel-footer text-right&quot;</span>&gt;</span></span><br><span class="line">      Closed</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>有关组件渲染的内容请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/ember-js-ru-men-zhi-nan-zhi-san-shi-bao-guo-nei-rong/">Ember.js 入门指南之三十包裹内容</a>。</p>
<p><strong>修改模板library-item-form</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- app&#x2F;templates&#x2F;components&#x2F;library-item-form.hbs --&gt;</span><br><span class="line">&lt;div class&#x3D;&quot;form-horizontal&quot;&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;form-group has-feedback &#123;&#123;if item.isValid &#39;has-success&#39;&#125;&#125;&quot;&gt;</span><br><span class="line">        &lt;label class&#x3D;&quot;col-sm-2 control-label&quot;&gt;Name*&lt;&#x2F;label&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;col-sm-10&quot;&gt;</span><br><span class="line">          &#123;&#123;input type&#x3D;&quot;text&quot; value&#x3D;item.name class&#x3D;&quot;form-control&quot; placeholder&#x3D;&quot;The name of the Library&quot;&#125;&#125;</span><br><span class="line">          &#123;&#123;#if item.isValid&#125;&#125;&lt;span class&#x3D;&quot;glyphicon glyphicon-ok form-control-feedback&quot;&gt;&lt;&#x2F;span&gt;&#123;&#123;&#x2F;if&#125;&#125;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;form-group&quot;&gt;</span><br><span class="line">        &lt;label class&#x3D;&quot;col-sm-2 control-label&quot;&gt;Address&lt;&#x2F;label&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;col-sm-10&quot;&gt;</span><br><span class="line">          &#123;&#123;input type&#x3D;&quot;text&quot; value&#x3D;item.address class&#x3D;&quot;form-control&quot; placeholder&#x3D;&quot;The address of the Library&quot;&#125;&#125;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;form-group&quot;&gt;</span><br><span class="line">        &lt;label class&#x3D;&quot;col-sm-2 control-label&quot;&gt;Phone&lt;&#x2F;label&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;col-sm-10&quot;&gt;</span><br><span class="line">          &#123;&#123;input type&#x3D;&quot;text&quot; value&#x3D;item.phone class&#x3D;&quot;form-control&quot; placeholder&#x3D;&quot;The phone number of the Library&quot;&#125;&#125;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;form-group&quot;&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;col-sm-offset-2 col-sm-10&quot;&gt;</span><br><span class="line">            &lt;button type&#x3D;&quot;submit&quot; class&#x3D;&quot;btn btn-default&quot; &#123;&#123;action &#39;buttonClicked&#39; item&#125;&#125; disabled&#x3D;&quot;&#123;&#123;unless item.isValid &#39;disabled&#39;&#125;&#125;&quot;&gt;&#123;&#123;buttonLabel&#125;&#125;&lt;&#x2F;button&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>
<p>注意观察上述代码与<code>libraries/new.hbs</code>和<code>libraries/edit.hbs</code>几乎是一样的。有点不一样的是把校验移到<code>model</code>中。比如校验<code>name</code>属性不为空。<br><strong>注意</strong>：顶部导入的代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Model <span class="keyword">from</span> <span class="string">&#x27;ember-data/model&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> attr <span class="keyword">from</span> <span class="string">&#x27;ember-data/attr&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Model.extend(&#123;</span><br><span class="line">  name: attr(<span class="string">&#x27;string&#x27;</span>),</span><br><span class="line">  address: attr(<span class="string">&#x27;string&#x27;</span>),</span><br><span class="line">  phone: attr(<span class="string">&#x27;string&#x27;</span>),</span><br><span class="line"></span><br><span class="line">  isValid: Ember.computed.notEmpty(<span class="string">&#x27;name&#x27;</span>)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>再修改<code>app/templates/libraries/index.hbs</code>引入组件。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>List<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></span><br><span class="line">  &#123;&#123;#each model as |library|&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-4&quot;</span>&gt;</span></span><br><span class="line">      &#123;&#123;#library-item item=library&#125;&#125;</span><br><span class="line">        &#123;&#123;#link-to &#x27;libraries.edit&#x27; library.id class=&#x27;btn btn-success btn-xs&#x27;&#125;&#125;Edit&#123;&#123;/link-to&#125;&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-danger btn-xs&quot;</span> &#123;&#123;<span class="attr">action</span> &#x27;<span class="attr">deleteLibrary</span>&#x27; <span class="attr">library</span>&#125;&#125;&gt;</span>Delete<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">      &#123;&#123;/library-item&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  &#123;&#123;/each&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>在迭代中使用组件，通过属性名<code>item</code>传递迭代出来的对象<code>library</code>到组件中。其中<code>link-to</code>和<code>button</code>这两句代码会替换到组件<code>library-item</code>的<code>{{yield}}</code>上。<br>等待项目重启完成，可以看到界面与之前的没有任何变化。页面是没有变化，但是后台的处理还需要完善。</p>
<p><strong>修改<code>app/templates/libraries/new.hbs</code></strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- app/templates/libraries/new.hbs --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>Add a new local Library<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-6&quot;</span>&gt;</span></span><br><span class="line">      &#123;&#123;library-item-form item=model buttonLabel=&#x27;Add to library list&#x27; action=&#x27;saveLibrary&#x27;&#125;&#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-4&quot;</span>&gt;</span></span><br><span class="line">      &#123;&#123;#library-item item=model&#125;&#125;</span><br><span class="line">          <span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">      &#123;&#123;/library-item&#125;&#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>修改<code>app/templates/libraries/edit.hbs</code></strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>Edit Library<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-6&quot;</span>&gt;</span></span><br><span class="line">      &#123;&#123;library-item-form item=model buttonLabel=&#x27;Save changes&#x27; action=&#x27;saveLibrary&#x27;&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-4&quot;</span>&gt;</span></span><br><span class="line">      &#123;&#123;#library-item item=model&#125;&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">      &#123;&#123;/library-item&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>在组件类<code>library-item-form.js</code>增加对<code>action</code>的处理。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">  buttonLabel: <span class="string">&#x27;Save&#x27;</span>,</span><br><span class="line"></span><br><span class="line">  actions: &#123;</span><br><span class="line"></span><br><span class="line">    buttonClicked(param) &#123;</span><br><span class="line">      <span class="built_in">this</span>.sendAction(<span class="string">&#x27;action&#x27;</span>, param);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h2 id="合并edit-hbs和new-hbs到form-hbs"><a href="#合并edit-hbs和new-hbs到form-hbs" class="headerlink" title="合并edit.hbs和new.hbs到form.hbs"></a>合并<code>edit.hbs</code>和<code>new.hbs</code>到<code>form.hbs</code></h2><p>原来的文件<code>edit.hbs</code>和<code>new.hbs</code>几乎是一样的，可以使用组件重构。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- /app/templates/libraries/form.hbs --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;title&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-6&quot;</span>&gt;</span></span><br><span class="line">      &#123;&#123;library-item-form item=model buttonLabel=buttonLabel action=&#x27;saveLibrary&#x27;&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-4&quot;</span>&gt;</span></span><br><span class="line">      &#123;&#123;#library-item item=model&#125;&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">      &#123;&#123;/library-item&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>为了实现代码复用，首先把不同的部分定义成属性：<code>title</code>、<code>buttonLabel</code>。默认情况下路由会渲染到同名的模板上，如果你想修改这个默认行为可以使用<code>renderTemplate()</code>方法。</p>
<h2 id="使用方法renderTemplate-和setupController"><a href="#使用方法renderTemplate-和setupController" class="headerlink" title="使用方法renderTemplate()和setupController()"></a>使用方法<code>renderTemplate()</code>和<code>setupController()</code></h2><p><strong>API介绍</strong></p>
<ul>
<li><a target="_blank" rel="noopener" href="http://emberjs.com/api/classes/Ember.Route.html#method_renderTemplate">renderTemplate()</a></li>
<li><a target="_blank" rel="noopener" href="http://emberjs.com/api/classes/Ember.Route.html#method_setupController">setupController()</a></li>
</ul>
<p>默认情况下路由会渲染到同名的模板上，我们使用方法<code>renderTemplate()</code>执行渲染的模板。比如下面的代码使用这个方法执行路由<code>new</code>渲染到模板<code>libraries/form.hbs</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/routes/libraries/new.js</span></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">  model: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.store.createRecord(<span class="string">&#x27;library&#x27;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  setupController: <span class="function"><span class="keyword">function</span> (<span class="params">controller, model</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>._super(controller, model);</span><br><span class="line"></span><br><span class="line">    controller.set(<span class="string">&#x27;title&#x27;</span>, <span class="string">&#x27;Create a new library&#x27;</span>);</span><br><span class="line">    controller.set(<span class="string">&#x27;buttonLabel&#x27;</span>, <span class="string">&#x27;Create&#x27;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  renderTemplate() &#123;</span><br><span class="line">    <span class="built_in">this</span>.render(<span class="string">&#x27;libraries/form&#x27;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  actions: &#123;</span><br><span class="line"></span><br><span class="line">    saveLibrary(newLibrary) &#123;</span><br><span class="line">      newLibrary.save().then(<span class="function">() =&gt;</span> <span class="built_in">this</span>.transitionTo(<span class="string">&#x27;libraries&#x27;</span>));</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    willTransition() &#123;</span><br><span class="line">      <span class="keyword">let</span> model = <span class="built_in">this</span>.controller.get(<span class="string">&#x27;model&#x27;</span>);</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (model.get(<span class="string">&#x27;isNew&#x27;</span>)) &#123;</span><br><span class="line">        model.destroyRecord();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>注意方法<code>setupController()</code>设置组件模板中的属性<code>title</code>和<code>buttonLabel</code>的值。同样的在修改路由<code>edit.js</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/routes/libraries/edit.js</span></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">  model(params) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.store.findRecord(<span class="string">&#x27;library&#x27;</span>, params.library_id);</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  setupController(controller, model) &#123;</span><br><span class="line">    <span class="built_in">this</span>._super(controller, model);</span><br><span class="line"></span><br><span class="line">    controller.set(<span class="string">&#x27;title&#x27;</span>, <span class="string">&#x27;Edit library&#x27;</span>);</span><br><span class="line">    controller.set(<span class="string">&#x27;buttonLabel&#x27;</span>, <span class="string">&#x27;Save changes&#x27;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  renderTemplate() &#123;</span><br><span class="line">    <span class="built_in">this</span>.render(<span class="string">&#x27;libraries/form&#x27;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  actions: &#123;</span><br><span class="line"></span><br><span class="line">    saveLibrary(newLibrary) &#123;</span><br><span class="line">      newLibrary.save().then(<span class="function">() =&gt;</span> <span class="built_in">this</span>.transitionTo(<span class="string">&#x27;libraries&#x27;</span>));</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    willTransition(transition) &#123;</span><br><span class="line">      <span class="keyword">let</span> model = <span class="built_in">this</span>.controller.get(<span class="string">&#x27;model&#x27;</span>);</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (model.get(<span class="string">&#x27;hasDirtyAttributes&#x27;</span>)) &#123;</span><br><span class="line">        <span class="keyword">let</span> confirmation = confirm(<span class="string">&quot;Your changes haven&#x27;t saved yet. Would you like to leave this form?&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (confirmation) &#123;</span><br><span class="line">          model.rollbackAttributes();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          transition.abort();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>使用组件重构之后可以删除<code>app/templates/libraries/new.hbs</code>和<code>app/templates/libraries/edit.hbs</code>，这两个文件不需要了。效果截图如下：</p>
<p><img src="/image/blog-image/182.png" alt="页面重构截图"></p>
<h2 id="使用组件nav-link-to重构-lt-li-gt-lt-a-gt-lt-a-gt-lt-li-gt"><a href="#使用组件nav-link-to重构-lt-li-gt-lt-a-gt-lt-a-gt-lt-li-gt" class="headerlink" title="使用组件nav-link-to重构&lt;li&gt;&lt;a&gt;&lt;/a&gt;&lt;/li&gt;"></a>使用组件<code>nav-link-to</code>重构<code>&lt;li&gt;&lt;a&gt;&lt;/a&gt;&lt;/li&gt;</code></h2><p>知道组件如何使用之后我们继续重构项目代码，重构导航模板<code>navbar.hbs</code>的链接代码。使用Ember CLI命令创建组件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ember g component nav-link-to</span><br></pre></td></tr></table></figure>
<p>这次使用扩展的方式扩展一个组件类，扩展Ember内置的组件类<code>LinkComponent</code>，使用方法<code>extend()</code>扩展一个类。然后使用属性<code>tagName</code>指定渲染之后的标签。更多有关组件属性的介绍请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/ember-js-ru-men-zhi-nan-zhi-san-shi-zi-ding-yi-bao-guo-zu-jian-de-htmlbiao-qian/">Ember.js 入门指南之三十一自定义包裹组件的HTML标签</a>，当然你也可以参考网址的教程实现本文的需求。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/components/nav-link-to.js</span></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.LinkComponent.extend(&#123;</span><br><span class="line">  tagName: <span class="string">&#x27;li&#x27;</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>：记得修改<code>Ember.Component.extend</code>为<code>Ember.LinkComponent.extend</code>。组件模板很简单。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- app/templates/components/nav-link-to.hbs --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;&quot;</span>&gt;</span>&#123;&#123;yield&#125;&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>最后在修改导航模板<code>navbar.hbs</code>为如下内容：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- app/templates/navbar.hbs --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">&quot;navbar navbar-inverse&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container-fluid&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;navbar-header&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">class</span>=<span class="string">&quot;navbar-toggle collapsed&quot;</span> <span class="attr">data-toggle</span>=<span class="string">&quot;collapse&quot;</span> <span class="attr">data-target</span>=<span class="string">&quot;#main-navbar&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;sr-only&quot;</span>&gt;</span>Toggle navigation<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">      &#123;&#123;#link-to &#x27;index&#x27; class=&quot;navbar-brand&quot;&#125;&#125;Library App&#123;&#123;/link-to&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;collapse navbar-collapse&quot;</span> <span class="attr">id</span>=<span class="string">&quot;main-navbar&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;nav navbar-nav&quot;</span>&gt;</span></span><br><span class="line">            &#123;&#123;#nav-link-to &#x27;index&#x27;&#125;&#125;Home&#123;&#123;/nav-link-to&#125;&#125;</span><br><span class="line">            &#123;&#123;#nav-link-to &#x27;libraries&#x27;&#125;&#125;Libraries&#123;&#123;/nav-link-to&#125;&#125;</span><br><span class="line">            &#123;&#123;#nav-link-to &#x27;about&#x27;&#125;&#125;About&#123;&#123;/nav-link-to&#125;&#125;</span><br><span class="line">            &#123;&#123;#nav-link-to &#x27;contact&#x27;&#125;&#125;Contact&#123;&#123;/nav-link-to&#125;&#125;</span><br><span class="line">      <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line">      <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;nav navbar-nav navbar-right&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;dropdown&quot;</span>&gt;</span></span><br><span class="line">              <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">&quot;dropdown-toggle&quot;</span> <span class="attr">data-toggle</span>=<span class="string">&quot;dropdown&quot;</span> <span class="attr">role</span>=<span class="string">&quot;button&quot;</span> <span class="attr">aria-haspopup</span>=<span class="string">&quot;true&quot;</span> <span class="attr">aria-expanded</span>=<span class="string">&quot;false&quot;</span>&gt;</span>Admin<span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;caret&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">              <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;dropdown-menu&quot;</span>&gt;</span></span><br><span class="line">                  &#123;&#123;#nav-link-to &#x27;admin.invitation&#x27;&#125;&#125;Invitations&#123;&#123;/nav-link-to&#125;&#125;</span><br><span class="line">                  &#123;&#123;#nav-link-to &#x27;admin.contact&#x27;&#125;&#125;Contacts&#123;&#123;/nav-link-to&#125;&#125;</span><br><span class="line">              <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span><span class="comment">&lt;!-- /.navbar-collapse --&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span><span class="comment">&lt;!-- /.container-fluid --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>等待项目重启完成，可以看到界面与之前的没有任何变化，可以任意点击导航栏菜单且不会出错。效果截图如下：</p>
<p><img src="/image/blog-image/183.png" alt="最终结果截图"></p>
<h2 id="家庭作业"><a href="#家庭作业" class="headerlink" title="家庭作业"></a>家庭作业</h2><p>本篇的家庭作业就是好好理解组件！参考下面的文章认真学习、理解组件。</p>
<ol>
<li><a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/ember-js-ru-men-zhi-nan-zhi-er-shi-ba-zu-jian-ding-yi/">Ember.js 入门指南之二十八组件定义</a></li>
<li><a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/ember-js-ru-men-zhi-nan-zhi-er-shi-jiu-shu-xing-chuan-di/">Ember.js 入门指南之二十九属性传递</a></li>
<li><a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/ember-js-ru-men-zhi-nan-zhi-san-shi-bao-guo-nei-rong/">Ember.js 入门指南之三十包裹内容</a></li>
<li><a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/ember-js-ru-men-zhi-nan-zhi-san-shi-zi-ding-yi-bao-guo-zu-jian-de-htmlbiao-qian/">Ember.js 入门指南之三十一自定义包裹组件的HTML标签</a></li>
<li><a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/chu-li-shi-jian/">Ember.js 入门指南之三十二处理事件</a></li>
<li><a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/04/07/action-chong-fa-bian-hua-2/">Ember.js 入门指南之三十三action触发变化</a></li>
</ol>
<br>
为了照顾懒人我把完整的代码放在[GitHub](https://github.com/ubuntuvim/library-app)上，如有需要请参考参考。博文经过多次修改，博文上的代码与github代码可能有出入，不过影响不大！如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/Ember-Demo/%E5%8F%91%E5%B8%83%E9%A1%B9%E7%9B%AE%EF%BC%8C%E5%8A%A0%E5%85%A5CRUD%E5%8A%9F%E8%83%BD/" rel="next" title="发布项目，加入CRUD功能">
          发布项目，加入CRUD功能
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/junit-jacoco/spring-junit-jacoco%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E6%B5%8B%E8%AF%95/" rel="prev" title="spring-junit-jacoco代码覆盖测试">
            spring-junit-jacoco代码覆盖测试
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B8%85%E7%90%86%E6%A8%A1%E6%9D%BF%EF%BC%8C%E4%BD%BF%E7%94%A8%E7%BB%84%E4%BB%B6%E9%87%8D%E6%9E%84"><span class="toc-text">清理模板，使用组件重构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%88%E5%B9%B6edit-hbs%E5%92%8Cnew-hbs%E5%88%B0form-hbs"><span class="toc-text">合并edit.hbs和new.hbs到form.hbs</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95renderTemplate-%E5%92%8CsetupController"><span class="toc-text">使用方法renderTemplate()和setupController()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%BB%84%E4%BB%B6nav-link-to%E9%87%8D%E6%9E%84-lt-li-gt-lt-a-gt-lt-a-gt-lt-li-gt"><span class="toc-text">使用组件nav-link-to重构&lt;li&gt;&lt;a&gt;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%B6%E5%BA%AD%E4%BD%9C%E4%B8%9A"><span class="toc-text">家庭作业</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '从服务器获取数据，引入组件', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
